package org.pi.common.plot.axes

import org.pi.common.geom.Point
import org.pi.common.plot.drawables.Drawable

class SimpleAxes(
    override var p1: Point,
    override var p2: Point
  ) extends BaseXyAxes {

  def this() = this(Point(0,0), Point(1,1))
  //def this(p1: Point, p2:Point) = this(p1,p2, AxisAdjustment.Manual, AspectRatio.Fixed, AxesOrientation.UpRight)

  protected override def getCoordinateTransform(bounds: java.awt.Rectangle) = {
    val (factorX, factorY, offsetX, offsetY) = getXyFactorAndXyOffset(bounds.width.toFloat, bounds.height.toFloat)
    (x:Float,y:Float) =>
    ( ((x-p1.x+offsetX)*factorX) + bounds.x,
      ((y-p1.y+offsetY)*factorY) + bounds.y )
  }

  protected override def getInverseCoordinateTransform(bounds: java.awt.Rectangle) = {
    val (factorX, factorY, offsetX, offsetY) = getXyFactorAndXyOffset(bounds.width.toFloat, bounds.height.toFloat)
    (x:Float,y:Float) =>
    ( (x-bounds.x)/factorX +p1.x-offsetX,
      (y-bounds.y)/factorY +p1.y-offsetY )
  }
  
  override def paint(graphics: java.awt.Graphics2D) {
	if (elements.length != 0) {
      if (autoAdjust == AxisAdjustment.Auto) doAutoAdjust
      initDefaultXTicksIfNotChangedByUser()
      initDefaultYTicksIfNotChangedByUser()
	}

	drawAxesAndInitClipBounds(graphics)
    val f = getCoordinateTransform(graphics.getClipBounds)
    elements.foreach( e => e.draw( f, graphics) )
  }

}

object SimpleAxes {
  def main(args: Array[String]) {
    import org.pi.common.plot.drawables._
    println("SimpleAxes demo");
    val axes = new SimpleAxes()
    axes.addDrawable( new Line(3,4,10,5) )
    axes.addDrawable( new Line(5,7,9,1) )
    println("SimpleAxes end");
  }
}